package controllers

import com.jfinal.aop.Before
import com.jfinal.aop.Clear
import com.jfinal.core.Controller
import controllers.interceptors.UserTokenCheckValidator
import services.CommentService

@Before(UserTokenCheckValidator::class)
class CommentController: Controller() {

    companion object {
        const val PARA_RECORD_ID = "record_id"
        const val PARA_COMMENT_ID = "comment_id"
        const val PARA_CONTENT = "content"
        const val PARA_TO_ID = "to_id"
        const val PARA_REPLY_ID = "reply_id"
    }

    private val service by lazy {
        CommentService(this)
    }

    fun create(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val recordId = getInt(PARA_RECORD_ID)
        val content = get(PARA_CONTENT)
        service.createMessage(userId, recordId, content)
    }

    fun reply(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val commentId = getInt(PARA_COMMENT_ID)
        val toUserId = getInt(PARA_TO_ID)
        val content = get(PARA_CONTENT)
        service.createSubMessage(commentId, userId, toUserId, content)
    }

    @Clear
    fun load(){
        val recordId = getInt(PARA_RECORD_ID)
        service.loadComment(recordId)
    }

    fun delete(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val commentId = getInt(PARA_COMMENT_ID)
        service.deleteComment(userId, commentId)
    }

    fun deleteReply(){
        val userId = UserTokenCheckValidator.getUserId(this)
        val replyId = getInt(PARA_REPLY_ID)
        service.deleteReply(userId, replyId)
    }
}